home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / tutil.arc / LIB001.INC < prev    next >
Encoding:
Text File  |  1984-07-09  |  6.7 KB  |  269 lines

  1. {.HE LIB001.INC             GetChar, GetString Procedures              Page #}
  2. {.FO                              Last Update of LIB001.INC : 08-09-84    DJS}
  3. {File : LIB001.INC      }
  4. { For : Turbo Pascal    }
  5. {  By : David J. Smith  }
  6. {Date : 07-09-84        }
  7.  
  8. Function GetChar : Char;
  9.  
  10. {This function will return a character from the keyboard.
  11.  Escape Codes are returned in one character, being the the second
  12.  character in the escape sequence with the high bit set chr(chr2 + 128).
  13.  the keys <Alt> 9, <Alt> 0, <Alt> -, <Alt> +, and <Ctrl> <PgUp> Will
  14.  return the codes 0 thru 4 since their key codes are greater than 128}
  15.  
  16. Const
  17.   ESC = #027;
  18.  
  19. Var
  20.   Ch1, Ch2 : Char;
  21.  
  22. begin {GetChar}
  23.   Read(Kbd, ch1);
  24.   if Ch1 = ESC then
  25.     if keypressed then
  26.       begin
  27.         read(Kbd, Ch2);
  28.         Ch1 := chr(ord(Ch2) + 128)
  29.       End;  {If}
  30.   GetChar := Ch1
  31. end;  {GetChar}
  32.  
  33.  
  34. {.PA}
  35. Type InStringType = String[80];        {Required for GetString}
  36.  
  37. Procedure GetString(Var InString : InStringType; Picture : InStringType;
  38.                     var Result : Char);
  39.  
  40. {Syntax :   GetString(InString, Picture, Result);
  41.  
  42.             Where InString and Picture are of type InStringType (String[80])
  43.             and Result is of type Char.
  44.  
  45. GetString will get input from Kbd and validate it against the String 'Picture'
  46. as Follows
  47.      '9'  --  Allow Numeric data to be entered
  48.      'A'  --  Allow only Alphabetic Data to be entered
  49.      '!'  --  Allow any character, Alpha converted to upper case
  50.      ' '  --  Allow any character
  51.  all others will be preserved from the picture to the Instring Variable.
  52.  Escape will exit the routine leaving InString Unchanged.}
  53.  
  54. Const
  55.   SP = ' ';   {Space}             BS = #008;   {Back Space);
  56.   CR = #013;  {Carraige Return}   ESC = #027;  {The Escape Character}
  57.   LeftArrow = #203;               RightArrow = #205;
  58.   Del = #211; {Del Key Code}      Ins = #210;  {Ins Key Code}
  59.   BEL = #007; {Bell}
  60.  
  61. Type
  62.   CharSet = Set of Char;
  63.  
  64. Var
  65.   Ch : Char;
  66.   Row, StartCol, StopCol, Pos : byte;
  67.   WorkString : InStringType;
  68.   Next, Done : Boolean;
  69.   DigitChar, AlphaChar, PicChar, CntChar : CharSet;
  70.  
  71.  
  72. {Subsidiary Procedures}
  73.  
  74. Procedure Initialize;
  75.  
  76. var Loop : Byte;
  77.  
  78. begin
  79.   Row := WhereY;
  80.   StartCol := WhereX;
  81.   Done := Length(Picture) < Length(InString);
  82.   If Done
  83.     then
  84.       begin
  85.         GotoXY(1,24);
  86.         Write(BEL,'ERROR : Instring Longer than Picture');
  87.         Delay(2000);
  88.         GotoXY(1,24);
  89.         Write(SP:40);
  90.         GotoXY(StartCol,Row)
  91.       end {Then}
  92.     else
  93.       Begin
  94.         DigitChar := ['0'..'9','.','+','-','e','E'];
  95.         AlphaChar := ['a'..'z','A'..'Z'];
  96.         PicChar := ['9','A','!',' '];
  97.         CntChar := [chr(000)..chr(031),chr(128)..chr(255)];
  98.         StopCol := StartCol + Length(Picture) - 1;
  99.         WorkString := '';
  100.         For Loop := 1 to Length(InString) do
  101.           if Picture[loop] in PicChar
  102.             then WorkString := WorkString + Instring[Loop]
  103.             else WorkString := WorkString + Picture[Loop];
  104.         For Loop := (Length(InString) + 1) to Length(Picture) do
  105.           if Picture[Loop] in PicChar
  106.             then WorkString := WorkString + SP
  107.             else WorkString := WorkString + Picture[Loop];
  108.         Pos := 1
  109.       end {else}
  110. end;  {Initialize}
  111.  
  112.  
  113. Procedure WriteString(Var PrintString : InStringType);
  114.  
  115. var
  116.   Loop, X, Y : Byte;
  117.  
  118. begin
  119.   X := WhereX;
  120.   Y := WhereY;
  121.   For Loop := 1 to Length(Picture) do
  122.     If Not(Picture[Loop] in PicChar)
  123.       then PrintString[Loop] := Picture[Loop];
  124.   GotoXY(StartCol,Row);
  125.   Write(PrintString);
  126.   GotoXY(X,Y)
  127. End;  {WriteString}
  128.  
  129.  
  130. Procedure MoveLeft;
  131.  
  132. begin
  133.   Repeat
  134.     if WhereX > StartCol
  135.       then GotoXY(WhereX-1, WhereY);
  136.     Pos := WhereX - StartCol + 1
  137.   Until (Picture[Pos] in PicChar) or (WhereX = StartCol);
  138.   Next := True
  139. end;  {Move Left}
  140.  
  141.  
  142. Procedure MoveRight;
  143.  
  144. begin
  145.   repeat
  146.     if WhereX < StopCol
  147.       then GotoXY(WhereX+1,WhereY);
  148.     Pos := WhereX - StartCol + 1
  149.   Until (Picture[Pos] in PicChar) or (WhereX = StopCol);
  150.   Next := True
  151. end;  {Move Right}
  152.  
  153.  
  154. Procedure DeleteChar;
  155. {Delete Character at Current Position}
  156.  
  157. begin
  158.   Delete(Workstring,Pos,1);
  159.   WorkString := Workstring + SP;
  160.   WriteString(WorkString);
  161.   Next := True
  162. end;  {DeleteChar}
  163.  
  164.  
  165. Procedure InsertChar;
  166. {Insert Space at Current Position}
  167.  
  168. Begin
  169.   Delete(WorkString,Length(WorkString),1);
  170.   Insert(SP,WorkString,Pos);
  171.   WriteString(WorkString);
  172.   Next := True
  173. end;  {InsertChar}
  174.  
  175.  
  176. Procedure AcceptCh;
  177.  
  178. begin
  179.   WorkString[Pos] := Ch;
  180.   MoveRight;
  181.   Next := True;
  182.   WriteString(WorkString)
  183. end;  {AcceptCh}
  184.  
  185.  
  186. Procedure ContCh;
  187.  
  188. begin
  189.   case Ch of
  190.             BS : begin
  191.                    MoveLeft;
  192.                    Ch := SP;
  193.                    AcceptCh;
  194.                    MoveLeft
  195.                  end;
  196.  
  197.     RightArrow : MoveRight;
  198.  
  199.      LeftArrow : MoveLeft;
  200.  
  201.            Ins : InsertChar;
  202.  
  203.            Del : DeleteChar;
  204.  
  205.            ESC : Begin
  206.                    Next := True;
  207.                    Done := True;
  208.                    Result := ESC
  209.                  end;  {ESC}
  210.  
  211.   Else
  212.     Begin
  213.       Next := True;
  214.       Done := True;
  215.       InString := WorkString;
  216.       Result := Ch
  217.     end  {Else}
  218.   End  {Case}
  219. End;  {ContCh}
  220.  
  221.  
  222. Begin {GetString -- At Last!}
  223.   Initialize;
  224.   if not done then
  225.     begin
  226.       TextColor(Black);
  227.       TextBackground(LightGray);
  228.       WriteString(WorkString);
  229.       If not (Picture[Pos] in PicChar) then MoveRight;
  230.       While not done do
  231.         begin
  232.           Next := False;
  233.           Case Picture[Pos] of
  234.             '9' : Repeat
  235.                     Ch := GetChar;
  236.                     If Ch in DigitChar then AcceptCh
  237.                     Else if Ch in CntChar then ContCh
  238.                     Else Write(BEL)
  239.                   Until Next;
  240.             'A' : Repeat
  241.                     Ch := GetChar;
  242.                     If Ch in AlphaChar then AcceptCh
  243.                     Else if Ch in CntChar then ContCh
  244.                     Else Write(BEL)
  245.                   Until Next;
  246.             '!' : Begin
  247.                     Ch := UpCase(GetChar);
  248.                     If Ch in CntChar then ContCh
  249.                     Else AcceptCh
  250.                   End;
  251.             ' ' : Begin
  252.                     Ch := GetChar;
  253.                     If Ch in CntChar then ContCh
  254.                     Else AcceptCh
  255.                   End;
  256.             Else
  257.               begin
  258.                 Ch := GetChar;
  259.                 if Ch in CntChar then ContCh
  260.               end  {Else}
  261.           end  {Case}
  262.         end;  {While}
  263.       TextColor(White);
  264.       TextBackground(Black);
  265.       GotoXY(StartCol,Row);
  266.       Write(SP:Length(Picture));
  267.       WriteString(Instring)
  268.     End  {If Not Done}
  269. End;  {GetString}